/* eslint-disable vue/html-indent */
<template>
  <div class="xtx-pagination" v-bind="$attrs">
    <span v-if="showTotal" href="javascript:;" class="total" :title="`共${total}条`">
      共{{ total }}条
    </span>
    <a v-if="myCurrentPage == 1" href="javascript:;" class="disabled" title="上一页"> 上一页 </a>
    <a v-else href="javascript:;" @click="go(-1)">上一页</a>
    <a
      v-if="advancedToolbar && myTotal > 0"
      href="javascript:;"
      :class="[myCurrentPage <= 1 && 'btn-disabled']"
      title="首页"
      @click="onHome"
    >
      首页
    </a>
    <span v-if="pageInfo.start > 2">...</span>
    <!-- eslint-disable-next-line prettier/prettier -->
    <a
      v-for="(item, idx) in pageInfo.pager"
      :key="idx"
      href="javascript:;"
      :class="{ active: myCurrentPage === item }"
      :title="`第${item}页`"
      @click="changePage(item)"
    >
      {{ item }}
    </a>
    <span v-if="pageInfo.end < pageInfo.pageCount">...</span>
    <!-- eslint-disable-next-line prettier/prettier -->
    <input
      v-if="advancedToolbar && myTotal > 0"
      v-model="cNumber"
      class="xtx-pagination-input"
      type="number"
      @keyup.enter="onNumber"
    />
    <a
      v-if="advancedToolbar && myTotal > 0"
      href="javascript:;"
      :class="[myCurrentPage >= pageInfo.end && 'btn-disabled']"
      title="末页"
      @click="onMY"
    >
      末页
    </a>
    <a
      v-if="myCurrentPage == pageInfo.end || myTotal === 0"
      href="javascript:;"
      class="disabled"
      title="下一页"
    >
      下一页
    </a>
    <a v-else href="javascript:;" title="下一页" @click="go(1)">下一页</a>
  </div>
</template>
<script lang="ts">
import { computed, ref, watch } from 'vue'
export default {
  name: 'XtxPagination',
  props: {
    total: { type: Number, default: 100 },
    pageSize: { type: Number, default: 10 },
    currentPage: { type: Number, default: 1 },
    btnCount: { type: Number, default: 5 },
    showTotal: { type: Boolean, default: true },
    advancedToolbar: { type: Boolean, default: true }
  },
  emits: ['currentChange'],
  setup(props, { emit }) {
    // 1. myTotal 总条数
    // 2. myPageSize 每页共几条
    // 3. myCurrentPage 当前第几页
    // 4. btnCount 页码按钮的数量
    // 5. pager:  页码数组
    const myTotal = ref(0) // 总条数
    const myPageSize = ref(5) // 每页共几条

    const myCurrentPage = ref(1) // 用户实时点击，修改

    const myBtnCount = ref(5) // 分页按钮的个数5个

    watch(
      props,
      () => {
        myTotal.value = props.total
        myPageSize.value = props.pageSize
        myCurrentPage.value = props.currentPage
        myBtnCount.value = props.btnCount
      },
      { immediate: true }
    )

    // 让当前的页码处于正中间
    // const pager = ref([1, 2, 3, 4, 5])

    // 根据上边信息，实时计算 pager，起始页码，结束页码
    const pageInfo = computed(() => {
      // 总页数
      const pageCount = Math.ceil(myTotal.value / myPageSize.value) // 一共有几页

      // 起点
      let start = myCurrentPage.value - Math.floor(myBtnCount.value / 2)
      // 终点
      let end = start + myBtnCount.value - 1

      // 意外1
      if (start < 1) {
        start = 1
        end = myBtnCount.value > pageCount ? pageCount : myBtnCount.value
      }

      // 意外2
      if (end > pageCount) {
        end = pageCount // 最大的页码
        start = end - myBtnCount.value + 1 < 1 ? 1 : end - myBtnCount.value + 1
      }

      const pager = []
      for (let i = start; i <= end; i++) {
        pager.push(i as never)
      }

      return { pager, start, end, pageCount }
    })

    const onHome = () => {
      if (myCurrentPage.value == 1) {
        return
      }
      myCurrentPage.value = 1
      emit('currentChange', myCurrentPage.value)
    }
    const onMY = () => {
      if (myCurrentPage.value == pageInfo.value.pageCount) {
        return
      }
      myCurrentPage.value = pageInfo.value.pageCount
      emit('currentChange', myCurrentPage.value)
    }

    const changePage = (page: any) => {
      if (page === myCurrentPage.value) {
        return
      }
      myCurrentPage.value = page
      console.log(page)
      emit('currentChange', page)
    }
    const go = (step: any) => {
      myCurrentPage.value = myCurrentPage.value + step
      emit('currentChange', myCurrentPage.value)
    }

    const cNumber = ref()
    const onNumber = () => {
      if (cNumber.value) {
        if (cNumber.value <= pageInfo.value.pageCount && cNumber.value > 0) {
          myCurrentPage.value = cNumber.value
        } else {
          if (cNumber.value <= 0) {
            // myCurrentPage.value = 1;
            return
          }
          myCurrentPage.value = pageInfo.value.pageCount
        }
        emit('currentChange', myCurrentPage.value)
      } else {
        if (cNumber.value === 0) {
          myCurrentPage.value = 1
          emit('currentChange', myCurrentPage.value)
        }
      }

      cNumber.value = undefined
    }

    return {
      myTotal,
      myPageSize,
      myCurrentPage,
      myBtnCount,
      pageInfo,
      changePage,
      go,
      onHome,
      onMY,
      cNumber,
      onNumber
    }
  }
}
</script>
<style scoped lang="scss">
.xtx-pagination {
  display: flex;
  // position: absolute;
  z-index: 999;
  justify-content: flex-end;
  padding: 8vh * $Bh 5vw * $Bw;
  color: #fff;
  // right: 0;
  font-size: 14vh * $Bh;

  > a {
    display: inline-block;
    padding: 5vh * $Bh 10vw * $Bw;
    border: 1vw * $Bw solid #e4e4e4;
    border-radius: 4vh * $Bh;
    margin-right: 10vw * $Bw;
    color: #fff;

    &:hover {
      color: #000;
      background-color: #fff;
    }

    &.active {
      color: #000;
      background-color: #fff;
    }

    &.disabled,
    &.btn-disabled {
      cursor: not-allowed;
      opacity: 0.4;

      &:hover {
        color: #fff;
        background-color: unset;
      }
    }
  }

  .xtx-pagination-input {
    display: inline-block;
    line-height: 14vh * $Bh;
    color: white;
    background: transparent;
    width: 50vh * $Bh;
    border: 1vh * $Bh solid #4084f8;
    border-radius: 5vh * $Bh;
    padding-left: 5vh * $Bh;
    font-size: initial;
    margin-right: 8vh * $Bh;
    outline: none;

    &::-webkit-outer-spin-button,
    &::-webkit-inner-spin-button {
      -webkit-appearance: none;
    }
  }

  input[type='number'] {
    -moz-appearance: textfield;
  }

  .total {
    color: #fff;
    padding: 5vh * $Bh 10vw * $Bw;
  }

  > span {
    margin-right: 10vw * $Bw;
  }
}
</style>
